package com.ctrlspace.core.managers;

import com.ctrlspace.core.dao.SessionDao;
import com.ctrlspace.core.dao.UserDao;
import com.ctrlspace.core.util.Security;
import com.ctrlspace.model.User;

public class SessionManager {
        
        private static SessionManager _instance;        
        private final SessionDao sessionDao = new SessionDao();
        private final UserDao userDao = new UserDao();
        
        private SessionManager() {}

        public static SessionManager getInstance() {
                if (_instance == null) {
                        _instance = new SessionManager();
                }
                
                return _instance;
        }

        public User loginUser(String username, String password) throws Exception {
                User user = null;
                
                // Check first if user name is taken
                boolean userExists = userDao.userExists(username);

                if (userExists) {
                        // TODO: Add custom exception (?) or just return null
                        
                        // Hash passwords
                        String hashedPassword = password;
                        hashedPassword = Security.SHA1(password);
                        
                        user = sessionDao.loginUser(username, hashedPassword);
                }
                
                return user;
        }
        
}
